using Avalonia.BiliBili.ViewModels;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using System.Diagnostics;

namespace Avalonia.BiliBili.Windows
{
    public partial class LoginWindow : WindowBase
    {
        private readonly LoginViewModel viewModel;
        public LoginWindow()
        {
            InitializeComponent();
            Width = 480;
            Height = 320;
            viewModel = new LoginViewModel(this);
            viewModel.Callback += Model_Callback;
            DataContext = viewModel;
        }
        private void LoginChange(object? sender, RoutedEventArgs e)
        {
            RunOnUiThread(async () =>
            {
                if (sender is RadioButton button)
                {
                    switch (button.Tag.ToString().Trim())
                    {
                        case "qr":
                            qrLogin.IsVisible = true;
                            smsLogin.IsVisible = false;
                            pwdLogin.IsVisible = false;
                            viewModel.LoginType = 0;
                            await viewModel.QrLogin();
                            break;
                        case "sms":
                            qrLogin.IsVisible = false;
                            smsLogin.IsVisible = true;
                            pwdLogin.IsVisible = false;
                            viewModel.LoginType = 1;
                            var idx = await viewModel.GetCountry();
                            countryBox.SelectedIndex = idx;
                            break;
                        default:
                            qrLogin.IsVisible = false;
                            smsLogin.IsVisible = false;
                            pwdLogin.IsVisible = true;
                            viewModel.LoginType = 2;
                            break;
                    }
                }
            });
        }
        private void Model_Callback(object sender)
        {
            RunOnUiThread(Close);
        }
        private void PwdBox_LostFocus(object? sender, RoutedEventArgs e)
        {
            ic33Hide.IsVisible = false;
            ic22Hide.IsVisible = false;
            ic33.IsVisible = true;
            ic22.IsVisible = true;
        }
        private void PwdBox_Focus(object? sender, GotFocusEventArgs e)
        {
            ic33.IsVisible = false;
            ic22.IsVisible = false;
            ic33Hide.IsVisible = true;
            ic22Hide.IsVisible = true;
        }
        protected override void AfterInitializeComponent()
        {
            pwdBox.GotFocus += PwdBox_Focus;
            pwdBox.LostFocus += PwdBox_LostFocus;
            qrLogin.IsVisible = false;
            smsLogin.IsVisible = false;
        }
    }
}